/* 
 * Copyright 2012 Mikaël GUILLEMOT
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.g2de.gui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;

import org.g2de.display.MouseControlListener;

/**
 * boutton a image
 * 
 * @author Mikael GUILLEMOT
 * @version 1.0
 * @since
 * 
 */
public class UIPicBoutton extends AbstractUI {

	private final int DEFAULT = 16;
	private BufferedImage b = null;
	private BufferedImage a = null;
	private BufferedImage used = null;

	public UIPicBoutton(BufferedImage base, BufferedImage hover) {
		super();
		// On cree une image de base en cas de problemes
		used = new BufferedImage(DEFAULT, DEFAULT, BufferedImage.TYPE_3BYTE_BGR);
		Graphics g = used.getGraphics();
		g.setColor(Color.GRAY);
		g.fillRect(0, 0, DEFAULT, DEFAULT);
		g.setColor(Color.RED);
		g.drawLine(0, 0, DEFAULT, DEFAULT);
		g.drawLine(DEFAULT, 0, 0, DEFAULT);
		g.setColor(Color.BLACK);
		g.drawRect(0, 0, DEFAULT, DEFAULT);
		setPic(base);
		setActivePic(hover);
	}

	/**
	 * initialise l'image de base du bouton
	 * 
	 * @param base
	 */
	protected void setPic(BufferedImage base) {
		if (base != null) {
			b = base;
			this.setSize(b.getWidth(), b.getHeight());
			used = b;
		}
	}

	/**
	 * initialise l'image active du bouton
	 * 
	 * @param base
	 */
	protected void setActivePic(BufferedImage active) {
		if (active != null && b.getWidth() == active.getWidth() && b.getHeight() == active.getHeight())
			a = active;
	}

	@Override
	protected void selfBehavior(MouseEvent arg0, short action) {
		if (action == MouseControlListener.EV_PRESSED)
			;
		else if (action == MouseControlListener.EV_RELEASED)
			;// TODO action : particules
		else if (action == MouseControlListener.EV_CLICK) {
			// TODO fair un son de clic
			fireAction(this.hashCode(), MouseControlListener.EV_CLICK);
		} else if (action == MouseControlListener.EV_ENTER)
			used = a;
		else if (action == MouseControlListener.EV_EXITED)
			used = b;

	}

	@Override
	protected void selfRender(Graphics2D buffer) {
		buffer.drawImage(used, activeZone.x, activeZone.y, null);
	}

	// public void setPosition(int x, int y) {
}
